package com.game.module.skill
{
	import com.dusk.util.McUtil;
	import com.dusk.util.ResUtil;
    import com.dusk.util.StringUtil;
    import com.dusk.util.TextUtil;
    import com.game.data.vo.PlayerDataVO;
    import com.game.data.vo.SkillVO;
    import com.game.manager.NotificationManager;
	import com.game.view.component.icon.RoleSkillIcon;
	import com.game.view.layer.LayerBase;
    
    import flash.display.MovieClip;
    import flash.display.SimpleButton;
    import flash.events.MouseEvent;
    import flash.geom.Point;
    import flash.text.TextField;
    
    //noinspection SpellCheckingInspection
	public class ActiveSkill extends LayerBase
	{
		
		public function ActiveSkill(data:PlayerDataVO)
		{
			super();
			player = data;
			sayinfo = new SayInfo();
		}
		
		public var upgrade_xfBtn:SimpleButton;
		
		public var xf1mc:MovieClip;
		public var xf2mc:MovieClip;
		public var leveltxt1:TextField;
		public var leveltxt2:TextField;
		public var lhtxt1:TextField;
		public var lhtxt2:TextField;
		public var xfname1:TextField;
		public var xfname2:TextField;
		
		public var upgrade1:SimpleButton;
		public var upgrade2:SimpleButton;
		public var upgrade3:SimpleButton;
		public var upgrade4:SimpleButton;
		public var upgrade5:SimpleButton;
		public var skillset1:SimpleButton;
		public var skillset2:SimpleButton;
		public var skillset3:SimpleButton;
		public var skillset4:SimpleButton;
		public var skillset5:SimpleButton;
		public var skillName1:TextField;
		public var skillName2:TextField;
		public var skillName3:TextField;
		public var skillName4:TextField;
		public var skillName5:TextField;
		public var skillIcon1:RoleSkillIcon;
		public var skillIcon2:RoleSkillIcon;
		public var skillIcon3:RoleSkillIcon;
		public var skillIcon4:RoleSkillIcon;
		public var skillIcon5:RoleSkillIcon;
		public var descript1:TextField;
		public var descript2:TextField;
		public var descript3:TextField;
		public var descript4:TextField;
		public var descript5:TextField;
		
		private var player:PlayerDataVO;
		private var whichxf:uint = 1;
		private var sayinfo:SayInfo;
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("ActiveSkill") as MovieClip;
			TextUtil.redrawTxt(mc);
			upgrade_xfBtn = mc.getChildByName("upgrade_xfBtn") as SimpleButton;
			
			var idx:int;
			for (idx = 1; idx <= 2; idx++)
			{
				this["xf" + idx + "mc"] = mc.getChildByName("xf" + idx + "mc") as MovieClip;
				this["leveltxt" + idx] = mc.getChildByName("leveltxt" + idx) as TextField;
				this["lhtxt" + idx] = mc.getChildByName("lhtxt" + idx) as TextField;
				this["xfname" + idx] = mc.getChildByName("xfname" + idx) as TextField;
			}
			for (idx = 1; idx <= 5; idx++)
			{
				this["upgrade" + idx] = mc.getChildByName("upgrade" + idx) as SimpleButton;
				this["skillset" + idx] = mc.getChildByName("skillset" + idx) as SimpleButton;
				this["skillName" + idx] = mc.getChildByName("skillName" + idx) as TextField;
				this["descript" + idx] = mc.getChildByName("descript" + idx) as TextField;
				this["skillIcon" + idx] = new RoleSkillIcon();
				with (this["skillIcon" + idx] as RoleSkillIcon)
				{
					name = "skillIcon" + idx;
					x = 380;
					y = 125 + 78 * (idx - 1);
				}
				mc.addChild(this["skillIcon" + idx] as RoleSkillIcon);
			}
			addChild(mc);
		}
		
		override protected function onAdded():void
		{
			xf1mc.buttonMode = true;
			xf2mc.buttonMode = true;
			xf1mc.addEventListener(MouseEvent.CLICK, firstXFFunc);
			xf2mc.addEventListener(MouseEvent.CLICK, secondXFFunc);
			xf1mc.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			upgrade_xfBtn.addEventListener(MouseEvent.CLICK, upGradeSkillFunc);
			setXFtxt();
			initXFIcon();
		}
		
		private function initStudySkill():void
		{
			var xfLevel:uint = player.hasStudySkill.xfLevel[whichxf - 1];
			for (var idx:int = 1; idx <= xfLevel; idx++)
			{
				with(this["skillset" + idx] as SimpleButton)
				{
					removeEventListener(MouseEvent.CLICK, skillsetFunc);
					addEventListener(MouseEvent.CLICK, skillsetFunc);
				}
				with(this["upgrade" + idx] as SimpleButton)
				{
					removeEventListener(MouseEvent.CLICK, skillupgradeFunc);
					addEventListener(MouseEvent.CLICK, skillupgradeFunc);
					removeEventListener(MouseEvent.ROLL_OVER, mOver);
					addEventListener(MouseEvent.ROLL_OVER, mOver);
					removeEventListener(MouseEvent.ROLL_OUT, mOut);
					addEventListener(MouseEvent.ROLL_OUT, mOut);
				}
				with(this["skillIcon" + idx] as RoleSkillIcon)
				{
					removeEventListener(MouseEvent.CLICK, studyNewSkill);
					addEventListener(MouseEvent.CLICK, studyNewSkill);
					changeState(RoleSkillIcon.CAN_LEARN);
				}
			}
			var studiedSkillList:Array = player.hasStudySkill.skillLevel[whichxf - 1];
			for (idx = 1; idx <= studiedSkillList.length; idx++)
			{
				var skillLv:int = studiedSkillList[idx - 1];
				if (skillLv > 0)
				{
					with (this["skillIcon" + idx] as RoleSkillIcon)
					{
						removeEventListener(MouseEvent.CLICK, studyNewSkill);
						getVO().skillLevel = skillLv;
						changeState(RoleSkillIcon.HAS_LEARN);
					}
				}
			}
			upgrade_xfBtn.visible = xfLevel < 5;
		}
		
		private function initXFIcon():void
		{
			for (var idx:int = 1; idx <= 2; idx++)
			{
				this["xf" + idx + "mc"].gotoAndStop(player.getRoleName() + "-yes");
			}
		}
		
		private function setXFtxt():void
		{
			var xfLevelArray:Array = player.hasStudySkill.xfLevel;
			for (var idx:int in xfLevelArray)
			{
				this["leveltxt" + (idx + 1)].text = "Lv." + xfLevelArray[idx];
				this["lhtxt" + (idx + 1)].text = findUpgradeXfNeedLhValue(xfLevelArray[idx]) + "";
				this["xfname" + (idx + 1)].text = findXFNameById(idx);
			}
		}
		
		private function findUpgradeXfNeedLhValue(param1:int):uint
		{
			const map:Object = {
				1: 100,
				2: 200,
				3: 500,
				4: 1000,
				5: 2000
			};
			return map[param1];
		}
		
		private function refreshSkillList():void
		{
			for (var i:int = 1; i <= 5; i++)
			{
				var vo:SkillVO = gc.configManager.skillProxy.getRoleSkillInGroupByIndex(player.roleId,whichxf,i);
				with (this["skillIcon" + i] as RoleSkillIcon)
				{
					setVO(vo);
					changeState(RoleSkillIcon.NOT_LEARN);
				}
				this["skillName" + i].text = vo.skillName;
				this["descript" + i].text = vo.skillDesc;
			}
		}
		
		private function findXFNameById(id:int):String
		{
			switch (player.roleId)
			{
				case 1:
					return id == 0 ? "斩系心法" : "火系心法";
				case 2:
					return id == 0 ? "愈系心法" : "水系心法";
				case 3:
					return id == 0 ? "御系心法" : "土系心法";
				case 4:
					return id == 0 ? "毒系心法" : "木系心法";
				case 5:
					return id == 0 ? "雷系心法" : "金系心法";
				default:
					return "错误的角色ID";
			}
		}
		
		override protected function onRemoved():void
		{
			xf1mc.removeEventListener(MouseEvent.CLICK, firstXFFunc);
			xf2mc.removeEventListener(MouseEvent.CLICK, secondXFFunc);
			upgrade_xfBtn.removeEventListener(MouseEvent.CLICK, upGradeSkillFunc);
			for (var i:int = 1; i <= 5; i++)
			{
				this["skillset" + i].removeEventListener(MouseEvent.CLICK, skillsetFunc);
				this["skillIcon" + i].removeEventListener(MouseEvent.CLICK, studyNewSkill);
				this["upgrade" + i].removeEventListener(MouseEvent.CLICK, skillupgradeFunc);
				this["upgrade" + i].removeEventListener(MouseEvent.ROLL_OVER, mOver);
				this["upgrade" + i].removeEventListener(MouseEvent.ROLL_OUT, mOut);
			}
			if (stage.getChildByName("SayInfo"))
			{
				stage.removeChild(sayinfo);
			}
		}
		
		private function mOver(param1:MouseEvent):void
		{
			var p:Point = new Point();
			var skillIndex:int = parseInt(StringUtil.getLastMultiChars(param1.currentTarget.name,1));
			if((this["skillIcon" + skillIndex] as RoleSkillIcon).getState() != RoleSkillIcon.HAS_LEARN)
			{
				return;
			}
			var skillLevel:int = player.hasStudySkill.skillLevel[whichxf - 1][skillIndex - 1];
			var vo:SkillVO = gc.configManager.skillProxy.getRoleSkillInGroupByIndex(player.roleId, whichxf, skillIndex);
			if(!sayinfo) sayinfo = new SayInfo();
			sayinfo.showtxt.text = "升级需要" + vo.getUpGradeCostByLevel(skillLevel) + "灵魂";
			p = new Point(param1.currentTarget.x, param1.currentTarget.y);
			p = param1.currentTarget.parent.localToGlobal(p);
			sayinfo.x = p.x;
			sayinfo.y = p.y;
			stage.addChild(sayinfo);
		}
		
		private function mOut(param1:MouseEvent):void
		{
			sayinfo.removeFromParent();
		}
		
		private function firstXFFunc(param1:MouseEvent):void
		{
			upgrade_xfBtn.x = 136.95;
			upgrade_xfBtn.y = 191.35;
			whichxf = 1;
			refreshSkillList();
			initStudySkill();
		}
		
		private function secondXFFunc(param1:MouseEvent):void
		{
			upgrade_xfBtn.x = 136.95;
			upgrade_xfBtn.y = 391.35;
			whichxf = 2;
			refreshSkillList();
			initStudySkill();
		}
		
		private function upGradeSkillFunc(param1:MouseEvent):void
		{
			var xfLevelArray:Array = player.hasStudySkill.xfLevel;
			var xfLevel:int = player.hasStudySkill.xfLevel[whichxf - 1];
			var needLhValue:int = findUpgradeXfNeedLhValue(xfLevel);
			if (xfLevel >= 5)
			{
				return;
			}
			if (player.lhValue < needLhValue)
			{
				NotificationManager.alert("灵魂不足");
				return;
			}
			xfLevelArray[whichxf - 1]++;
			player.reduceLhValue(needLhValue);
			refreshPaenl();
		}
		
		public function refreshPaenl():void
		{
			setXFtxt();
			refreshSkillList();
			initStudySkill();
			var panel:ManageSkill = McUtil.getClassInParents(this, ManageSkill);
			panel.refreshPanel();
		}
		
		private function skillsetFunc(evt:MouseEvent):void
		{
			var skillIndex:int = int(StringUtil.getLastMultiChars(evt.currentTarget.name,1));
			if((this["skillIcon" + skillIndex] as RoleSkillIcon).getState() != RoleSkillIcon.HAS_LEARN)
			{
				NotificationManager.alert("请先学习该技能");
				return;
			}
			var newVo:SkillVO = new SkillVO();
			var vo:SkillVO = gc.configManager.skillProxy.getRoleSkillInGroupByIndex(player.roleId, whichxf, skillIndex);
			newVo.updateFromObject(vo.cloneToObject());
			addChild(new SetSkillKey(player,newVo));
		}
		
		private function skillupgradeFunc(param1:MouseEvent):void
		{
			var skillIndex:int = parseInt(StringUtil.getLastMultiChars(param1.currentTarget.name, 1));
			var currentSkillLevel:int = player.hasStudySkill.skillLevel[whichxf - 1][skillIndex - 1];
			if((this["skillIcon" + skillIndex] as RoleSkillIcon).getState() != RoleSkillIcon.HAS_LEARN)
			{
				NotificationManager.alert("请先学习该技能");
				return;
			}
			var vo:SkillVO = gc.configManager.skillProxy.getRoleSkillInGroupByIndex(player.roleId, whichxf, skillIndex);
			if (currentSkillLevel >= vo.maxLevel)
			{
				NotificationManager.alert("技能等级已达上限");
				return;
			}
			/*if (player.level / 5 < currentSkillLevel + 1)
			{
				NotificationManager.alert("等级不足,需要升到" + (5 * (currentSkillLevel + 1)).toString() + "级才可升级技能");
				return;
			}*/
			if (player.lhValue < vo.getUpGradeCostByLevel(currentSkillLevel))
			{
				NotificationManager.alert("灵魂不足");
				return;
			}
			player.reduceLhValue(vo.getUpGradeCostByLevel(currentSkillLevel));
			NotificationManager.alert("技能等级提升");
			player.hasStudySkill.skillLevel[whichxf - 1][skillIndex - 1]++;
			player.updateEquippedSkill();
			initStudySkill();
			sayinfo.showtxt.text = "升级需要" + vo.getUpGradeCostByLevel(currentSkillLevel + 1) + "灵魂";
			refreshPaenl();
		}
		
		private function studyNewSkill(param1:MouseEvent):void
		{
			/*var skillLimit:int = Math.ceil(player.level / 6);
			if (player.getAllStudiedSkillNum() >= skillLimit)
			{
				NotificationManager.alert("每6级只能学习一个技能!");
				return;
			}*/
			var skillIndex:int = parseInt(StringUtil.getLastMultiChars(param1.currentTarget.name, 1));
			if((this["skillIcon" + skillIndex] as RoleSkillIcon).getState() != RoleSkillIcon.CAN_LEARN)
			{
				NotificationManager.alert("技能状态错误");
				return;
			}
			player.hasStudySkill.skillLevel[whichxf - 1][skillIndex - 1] = 1;
			with((this["skillIcon" + skillIndex] as RoleSkillIcon))
			{
				removeEventListener(MouseEvent.CLICK, studyNewSkill);
				changeState(RoleSkillIcon.HAS_LEARN)
			}
			var emptyIndex:Array = player.findWhichSkillIndexIsEmpty();
			if (emptyIndex.length > 0)
			{
				var newVo:SkillVO = new SkillVO();
				var vo:SkillVO = gc.configManager.skillProxy.getRoleSkillInGroupByIndex(player.roleId, whichxf, skillIndex);
				newVo.updateFromObject(vo.cloneToObject());
				vo.skillIndex = emptyIndex[0];
				vo.skillLevel = 1;
				player.equippedSkill.push(vo);
			}
			player.updateEquippedSkill();
			refreshPaenl();
			
			/*var _loc2_:* = 0;
			var _loc3_:* = null;
			var _loc4_:* = null;
			var _loc5_:* = null;
			if (player.howMuchSkillHasYouStudy() < player.skillLimit)
			{
				_loc2_ = uint(int(String(param1.currentTarget.name).substr(5, 1)));
				if (mainskillmc["skill" + _loc2_].currentFrame == 2)
				{
					_loc3_ = findSkillNameBySkillBtn(_loc2_);
					player.isstudyskill[whichxf - 1].skillName += _loc3_[0] + "~1|";
					if ((_loc4_ = player.findWhichSkillBtnNoneSet()) != "")
					{
						_loc5_ = {"skillName": _loc3_[0], "keys": _loc4_, "needLh": _loc3_[1], "slev": 1};
						player.skillbykey.push(_loc5_);
					}
					mainskillmc["skill" + _loc2_].removeEventListener(MouseEvent.CLICK, studyNewSkill);
					mainskillmc["skill" + _loc2_].gotoAndStop(3);
				}
			}
			else
			{
				NotificationManager.alert("您当前只能学习" + player.skillLimit + "个技能!");
			}*/
		}
	}
}
